/**
 * Project Name:YCApiServer-Api
 * File Name:YueShuoService.java
 * Package Name:com.yc.music.service.service103
 * Date:2016年5月20日上午11:36:04
 * Copyright (c) 2016 
 *
 */
package com.yc.music.service.service103;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import com.yc.music.common.context.YcContext;
import com.yc.music.common.util.BindingResultUtil;
import com.yc.music.common.util.RoomUtil;
import com.yc.music.context.YtConstant;
import com.yc.music.mapper.cache.YueShuoCacheMapper;
import com.yc.music.mapper.dao.YueshuoCommentMapper;
import com.yc.music.mapper.dao.YueshuoMapper;
import com.yc.music.mapper.dao.YueshuoZanMapper;
import com.yc.music.model.OptWork;
import com.yc.music.model.musicYueShuo.MuscicYueSVo;
import com.yc.music.model.musicYueShuo.MuscicYueShuoDetail;
import com.yc.music.model.musicYueShuo.MuscicYueShuoVo;
import com.yc.music.model.musicYueShuo.YueshuoZan;

/**
 * ClassName: YueShuoService <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2016年5月20日 上午11:36:04 <br/>
 *
 * @author panguixiang
 * @version
 */
@Service
public class YueShuoService {
	
	private static Logger logger = Logger.getLogger(WorkZanService.class);

	@Autowired 
	private YueShuoCacheMapper yueShuoCacheMapper;

	@Autowired
	private YueshuoMapper yueshuoMapper;
	
	@Autowired
	private YueshuoZanMapper yueshuoZanMapper;
	
	@Autowired
	private YueshuoCommentMapper yueshuoCommentMapper;
	
	
	

	@Value("${Pic_QiLiu}")
	private String pIC_QiLiu;
	
	@Value("${Audio_QiLiu}")
	private String audio_QiLiu;
	
	/**
	 * 
	 * yueshuoListPage:(乐说列表). <br/> 
	 * @author panguixiang
	 * @param page
	 * @param result
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public Map<String, Object> yueshuoListPage(Integer page, Map<String, Object> result) throws Exception {
		int start = 0, size = 10;
		if (page != null && page > 1) {
			start = (page - 1) * 10;
		}
		// 乐说列表
		List<MuscicYueSVo> yueshuoList = yueShuoCacheMapper.getYueShuoPages(2, 2, 1, start, size);
		if(yueshuoList!=null){
			for(MuscicYueSVo vo : yueshuoList) {
				vo.setPic(RoomUtil.jointDomain(vo.getPic(),pIC_QiLiu));
//				if(vo.getType()==1) {//1=mp3,2=web，wap页面
//					vo.setUrl(RoomUtil.jointDomain(vo.getUrl(),audio_QiLiu));
//				}
			}
		}
		result.put(YcContext.CODE, 200);
		result.put(YcContext.MESSAGE, YtConstant.SYS_SUCCSS_MSG);
		result.put(YcContext.DATA, yueshuoList);
		return result;
	}
	
	
	/**
	 * 
	 * yueshuoListPage:(乐说列表). <br/> 
	 * @author qinhaiguan
	 * @param page
	 * @param result
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 * @version v2.2
	 */
	public Map<String, Object> yueshuoListPage22(Integer page, Map<String, Object> result) throws Exception {
		int start = 0, size = 10;
		if (page != null && page > 1) {
			start = (page - 1) * 10;
		}
		// 乐说列表
		List<MuscicYueSVo> yueshuoList = yueShuoCacheMapper.getYueShuoPages22(2, 2, 1, start, size);
		if(yueshuoList!=null){
			for(MuscicYueSVo vo : yueshuoList) {
				vo.setPic(RoomUtil.jointDomain(vo.getPic(),pIC_QiLiu));
			}
		}
		result.put(YcContext.CODE, 200);
		result.put(YcContext.MESSAGE, YtConstant.SYS_SUCCSS_MSG);
		result.put(YcContext.DATA, yueshuoList);
		return result;
	}
	
	
	public Map<String, Object> yueshuoDetail22(Long itemid,Long uid, Map<String, Object> result) throws Exception {
		
		
		MuscicYueShuoVo yueshuoVo=yueshuoMapper.getYueShuoById22(itemid);
		MuscicYueShuoDetail detail=null;
		if (null!=yueshuoVo) {
			detail=new MuscicYueShuoDetail();
			detail.setZannum((long) yueshuoZanMapper.getYueshuoZanCount(itemid));//点赞数量
			detail.setCommentnum((long) yueshuoCommentMapper.getYueshuoCommentCount(itemid));//评论数量
			detail.setSharenum(yueshuoVo.getSharenum());//分享数量
			detail.setShareurl(yueshuoVo.getUrl());
		}
		
		//查询是否点过赞
		int count=yueshuoZanMapper.getYueshuoZanCountByUidItemid(uid, itemid);
		if (count>0) {
			detail.setIsZan(1);
		}else{
			detail.setIsZan(0);
		}
		
		result.put(YcContext.CODE, 200);
		result.put(YcContext.MESSAGE, YtConstant.SYS_SUCCSS_MSG);
		result.put(YcContext.DATA, detail);
		return result;
	}
	
	public Map<String,Object> yueshuoZan22(YueshuoZan yueshuoZan, BindingResult binding, Map<String,Object> result) throws Exception {
		String valiterMsg = BindingResultUtil.getBindErrorMsg(binding);
		if(StringUtils.isNotBlank(valiterMsg)) {
			result.put(YcContext.CODE, 400);
			result.put(YcContext.MESSAGE, valiterMsg);
			result.put(YcContext.DATA,YcContext.EMPTY_STRING);
			return result;
		}
		
		//先查询是否点过赞
		int count=yueshuoZanMapper.getYueshuoZanCountByUidItemid(yueshuoZan.getUid(), yueshuoZan.getItemid());
		logger.info("获取信息count"+count);
		if (count>0) {
			//如果赞过则直接删除关联记录
			yueshuoZanMapper.deleteYueshuoZan(yueshuoZan.getUid(), yueshuoZan.getItemid());
			logger.info("取消乐说点赞");
			result.put(YcContext.DATA,YtConstant.YUESHUPO_ZAN_CANCLE);
		}else{
			yueshuoZanMapper.saveYueshuoZan(yueshuoZan.getUid(), yueshuoZan.getItemid());
			logger.info("增加乐说点赞");
			result.put(YcContext.DATA,YtConstant.YUESHUPO_ZAN_SAVE);
		}
		
		result.put(YcContext.CODE, 200);
		result.put(YcContext.MESSAGE, YcContext.EMPTY_STRING);
		
		return result;
	}
	
	public Map<String, Object> yueshuoShareCount22(Long itemid, Map<String, Object> result) throws Exception {
		
		yueshuoMapper.updateYueshuoShareCount(itemid);
		
		result.put(YcContext.CODE, 200);
		result.put(YcContext.MESSAGE, YtConstant.SYS_SUCCSS_MSG);
		result.put(YcContext.DATA, YcContext.EMPTY_STRING);
		return result;
	}
	
}
